<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    :confirm-btn="null"
    width="80%"
    top="5%"
  >
    <t-card size="small" title="基本信息">
      <t-form :colon="true" labelWidth="160px">
        <t-row style="row-gap:24px">
          <t-col :span="12">
            <t-form-item label="上级单位" name="parentId">
                  <div v-if="!isUnitAdmin">
                    {{ row.parentId | filterByTree(menuOptions,label = "deptName") }}
                  </div>
                  <div v-else>
                    {{ deptName }}
                  </div>
<!--              <t-tree-select v-if="!isUnitAdmin" :disabled="isUnitAdmin" clearable filterable :treeProps="{keys: {value: 'id',label:'deptName'}}"-->
<!--                             v-model="formData.parentId" :data="menuOptions" placeholder="选择上级单位"></t-tree-select>-->
<!--              <t-input disabled v-else v-model="deptName"></t-input>-->
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="归口单位" name="managerId">
              {{ row.manageDeptId | filterByTree(menuOptions2) }}
<!--              <t-tree-select  clearable filterable :treeProps="{keys: {value: 'id'}}" v-model="formData.managerId"-->
<!--                              :data="menuOptions2" placeholder="选择归口单位"></t-tree-select>-->
              <!--            <t-input disabled v-else v-model="deptManagerName"></t-input>-->
            </t-form-item>
          </t-col>
          <t-col :span="6">
            <t-form-item label="单位名称" name="deptName">
              {{ formData.deptName }}
            </t-form-item>
          </t-col>
<!--          <t-col :span="6">-->
<!--            <t-form-item label="显示排序" name="orderNum">-->
<!--&lt;!&ndash;              <t-input-number clearable v-model="formData.orderNum" :min="1" placeholder="请输入"></t-input-number>&ndash;&gt;-->
<!--            </t-form-item>-->
<!--          </t-col>-->

          <t-col :span="12">
            <t-form-item label="单位地址" name="address">
              {{ formData.address }}
            </t-form-item>
          </t-col>

          <t-col :span="6">
            <t-form-item label="社会信用代码" name="code">
              {{ formData.code }}
            </t-form-item>
          </t-col>

          <t-col :span="6">
            <t-form-item label="邮编" name="postCode">
              {{ formData.postCode }}
            </t-form-item>
          </t-col>

          <t-col :span="6">
            <t-form-item label="负责人" name="leader">
              {{ formData.leader }}
            </t-form-item>
          </t-col>
          <t-col :span="6">
            <t-form-item label="联系电话" name="phone">
              {{ formData.phone }}
            </t-form-item>
          </t-col>
          <t-col :span="6">
            <t-form-item label="邮箱" name="email">
              {{ formData.email }}
            </t-form-item>
          </t-col>
          <t-col :span="6">
            <t-form-item label="单位状态" name="status">
              {{ formData.status | filterByDict(dict.type.sys_normal_disable) }}
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="单位简介" name="deptProfile">
              {{ formData.deptProfile }}
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
    </t-card>
    <t-card size="small" title="项目信息">

    </t-card>
    <t-card size="small" title="成果信息"></t-card>
  </t-dialog>
</template>

<script>
import { getDept } from "@/api/system/dept";


export default {
  name: "company-detail",
  dicts: ['sys_normal_disable'],
  props:{
    header:{
      type: String,
      default: '',
    },
    objId:{
      type: [Number, String],
    }
  },
  data(){
    return {
      visible: true,
      menuOptions: [],
      menuOptions2: [],
      deptName: '',
      deptManagerName: '',
      isUnitAdmin: false,
      formData: {
        parentId: null,
        managerId: null,
        address: '',
        code: '',
        postCode: '',
        deptName: "",
        leader: "",
        phone: "",
        email: "",
        status: "0",
        orderNum: 1,
        deptProfile: '',
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj(val);
        }
      }
    },
  },
  methods:{
    async getObj() {
      await Promise.all([this.getTreeselect(), this.getTreeselectAllIn()])
      getDept(this.deptId).then(response => {
        if (response.data.parentId === 0) {
          response.data.parentId = response.data.id;
        }
        if (response.data.managerId === 0) {
          response.data.managerId = response.data.id;
        }
        // 如果说当前单位是 列表的第一级 兼容 管理员单位
        const index = this.menuOptions.findIndex(val => this.deptId === val.id)
        if (index !== -1) {
          getDept(response.data.parentId).then(resp => {
            this.isUnitAdmin = true;
            this.deptName = resp.data.deptName;
          }).catch((e) => {
            this.$message.error(e.toString());
          });
        }
        this.formData = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>

<style scoped>

</style>
